﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TCPIPControl.StructHelper;
using TcpLabCommon;

namespace Common.GeneralClass
{
    public class AccDataInfo
    {
        public long Id;
        public short ChannelId;
        public float TotaldB;
        public float HighdB;
        public float LowdB;
        public float[] _1_3octavedB = new float[31];
        public float VelocityRMS;
        public float AverageVol;
        public float DisplacementRMS;
        public float OneMultiplierAmp;
        public float TwoMultiplierAmp;
        public float HalfMultiplierAmp;
        public byte AlarmFlag;
        public DateTime InsertTime;
        public short ProjectId;
        public float Rpm;
        public string DatapaketPath;
        public float[] FFTAmpDB = new float[65536];

    }

    public class OperateAccDataInfo
    {
        MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString());
        MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString());
        DateTime dateBegin = DateTime.Now;
        int insertcount = 0;
        public int AddAccDataInfo(AccData accdata)
        {
            int i = 0;
            using (mydb_insert)
            {
                try
                {
                    mydb_insert.Open();
                    Stopwatch sp = new Stopwatch();
                    sp.Start();
                    DateTime nowtime = Convert.ToDateTime(accdata.Year.ToString() + "-" + accdata.Month.ToString() + "-" + accdata.Day.ToString() + " " + accdata.Hour.ToString() + ":" + accdata.Minute.ToString()+":" + accdata.Second.ToString());
                    long rpmid = Mdl_Class.RpmId;
                    if (rpmid > 0)
                    {
                        i = mydb_insert.ExecuteNonQuery_AccData(rpmid, "T_AccData", nowtime, accdata);
                        if (Mdl_Class.IsSaveFFTAmp)
                        {
                            i = mydb_insert.ExecuteNonQuery_AccData_DataPaket(nowtime, accdata);
                        }
                        TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
                        TimeSpan ts2 = new TimeSpan(nowtime.Ticks);
                        TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                        if (ts3.TotalSeconds > 60)
                        {
                            i = mydb_insert.ExecuteNonQuery_AccData(rpmid, "T_AccData_Day", nowtime, accdata);
                            dateBegin = nowtime;
                        }

                        insertcount++;
                    }
                    sp.Stop();
                    Console.WriteLine("加速度 总计耗时:"+sp.ElapsedMilliseconds+" 插入条数："+ insertcount);
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddAccDataInfo()", ex.ToString());
                }
            }
            return i;
        }

        /// <summary>
        /// 通过字段名查询
        /// </summary>
        public List<float> GetDataByFieldName(string FieldName, int ProjectId, int channelId,string dateTime_Start, string dateTime_End, long RpmId,int alarmFlag) 
        {
            List<float> dataslist = new List<float>();
            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select {0} from {1} where 1=1 ", FieldName, "T_AccData");
         
                 

                        if (ProjectId > 0)
                        {
                            sql = sql + string.Format(" and projectId={0}", ProjectId);
                        }
                        if (RpmId > 0)
                        {
                            sql = sql + string.Format(" and RpmId={0}", RpmId);
                        }
                    if (alarmFlag>=0)
                    {
                        sql = sql + string.Format(" and AlarmFlag={0}", alarmFlag);
                    }
                    if (channelId >= 0)
                    {
                        sql = sql + string.Format(" and ChannelId={0}", channelId);
                    }

                    if (dateTime_Start != "")
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dateTime_Start, dateTime_End);
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        float testvalue = 0.0f;
                        if (mydb.DataReader[FieldName]!=null) {
                            testvalue = Convert.ToSingle(mydb.DataReader[FieldName].ToString());
                        }
                        dataslist.Add(testvalue);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    dataslist.Clear();
                    Mdl_Class.WriteLog.dbErrLog("OperateAccDataInfo.GetDataByFieldName(string FieldName, int ProjectId, int channelId,string dateTime_Start, string dateTime_End, long RpmId,int alarmFlag) ", ex.ToString());
                }
            }

            return dataslist;
        }



    }
}
